//
//  JXWelcomeViewController.swift
//  swift高仿微博
//
//  Created by peijiaxing on 16/2/27.
//  Copyright © 2016年 peijiaxing. All rights reserved.
//

import UIKit
import SDWebImage


class JXWelcomeViewController: UIViewController {

    override func loadView() {
         view = bgImageView
    }
    
    override func viewDidLoad() {
        
        
        super.viewDidLoad()
       
        
        setupUI()
        
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    private func startAnimation(){
        // 更新约束
        photoImageView.snp_updateConstraints { (make) -> Void in


            make.bottom.equalTo(self.view.snp_bottom).offset(-UIScreen.mainScreen().bounds.height + 300)
            make.centerX.equalTo(self.view.snp_centerX)
            
            make.size.equalTo(CGSize(width: 170 / 2, height: 170 / 2))
        }
        /**
        
        
        - parameter delay:                   延时
        - parameter usingSpringWithDamping:  减弱系数
        - parameter initialSpringVelocity:  初始速度
        - parameter options:                <#options description#>
        - parameter animations:             <#animations description#>
        - parameter completion:             <#completion description#>
        */
        UIView.animateWithDuration(2.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            //完成约束动画的时候,我们需要调用 -- 面试的时候,经常被问到的
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                // 完成之后
                // 切换视图
                NSNotificationCenter.defaultCenter().postNotificationName(kNotificationEnter, object: nil)
        }
        
    }
    
    
    private func setupUI(){
    
        view.addSubview(nameLabel)
        view.addSubview(photoImageView)
        
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-150)
            make.centerX.equalTo(view.snp_centerX)
            
            // 设置宽高
            make.size.equalTo(CGSizeMake(170 / 2, 170 / 2))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(photoImageView.snp_bottom).offset(10)
            make.centerX.equalTo(view.snp_centerX)
        }
        // 修改头像
        let url = NSURL(string: JXUserViewModel.shareInstance.model?.profile_image_url ?? "")
        
        if let  Ourl = url {
            photoImageView.sd_setImageWithURL(Ourl, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    
    // 背景图
    private lazy var bgImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "ad_background")
    
        return img
    }()
    
    ///  头像
    private lazy var photoImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
        
        img.layer.cornerRadius = 170 / 4
        // 裁剪多余的
        img.layer.masksToBounds = true
        img.layer.borderColor = UIColor.grayColor().CGColor
        
        img.layer.borderWidth = 2
        return img
        }()
    
    // 欢迎文字
    private lazy var nameLabel: UILabel = {
       let lab = UILabel()
        lab.textAlignment = .Center
        lab.font = UIFont.systemFontOfSize(15)
        lab.textColor = UIColor.darkGrayColor()
        
        let name = JXUserViewModel.shareInstance.model?.screen_name ?? ""
        lab.text = "\(name)欢迎回来"

        return lab
        
    }()
}
